﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using ESF.General;
using System.Net;
using ESF.Peer.Configuration;

#endregion


namespace ESF.Peer.Handlers
{
	public static class RequestHandlerFactory<M>
	{
		#region Funciones Estaticas
		
		public static IRequestHandler<M> GetRequestHandler (string pAppName)
		{
			PeerInfoSection srcConf;
			
			
			srcConf = ConfigurationManager.GetSection ("PeerInfo") as PeerInfoSection; 
			
			foreach (RequestHandlerElement rhe in srcConf.RequestHandlers)
			{
				if (pAppName == rhe.appName)
				{
					try
					{
						return (IRequestHandler<M>) Activator.CreateInstance (Type.GetType (rhe.type));
					}
					catch (Exception pEx)
					{
						throw new GenericException (GenericException.CRI_SYSTEM_ERROR, string.Format ("Type for Handler of {0} was not found: {1}", pAppName, rhe.type), pEx);
					}
				}
			}
			
			throw new GenericException (GenericException.CRI_SYSTEM_ERROR, string.Format ("Application {0} was not found for Handler", pAppName));
		}
		
		#endregion
	}
}
